package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {


    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
        return baseMapper.selectCategoryBrandList();
    }

    @Override
    public void add(CategoryBrand categoryBrand) {
        //1.判断查询品牌是否存在
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).
                eq(CategoryBrand::getId, categoryBrand.getId());
        long count=count(queryWrapper);
        if (count>0){
            throw new SecurityException("品牌已经存在");
        }
        //2.保存品牌
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
         baseMapper.insert(categoryBrand);
    }

    /**
     * 根据三级分类ID查询品牌列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> brandList(Long categoryId) {

        return baseMapper.brandList(categoryId);
    }


}
